package com.itheima.tliassystem.interceptor;

import com.google.gson.Gson;



import entity.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import com.itheima.tliassystem.uile.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class LoginIntercetor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Gson gson = new Gson();
        String notLogin = gson.toJson(Result.error("NOT_LOGIN"));

        String token = request.getHeader("Token");
        if (token == null) {
            response.getWriter().write(notLogin);
            return false;
        }
        try {
            JwtUtils.parseJWT(token);
        } catch (Exception e) {
            response.getWriter().write(notLogin);
            return false;
        }

        return true;
    }
}
